<?php
namespace app\admin\controller;

use app\Basic;
use app\Logined;
use \think\facade\Db;

class Crud extends Logined
{
    public function list()
    {
        $table=$this->param['table'];
        $table_key = $table . '_id';
        $filed = Db::name($table)->getTableFields();
        $fieldsType=Db::name($table)->getFieldsType();
        $join_table=$this->param['join_table']??null;

        $db = Db::name($this->param['table']);

        if(in_array('sort_num',$filed)){
            $db->order("sort_num",'desc');
        }
        if(in_array('add_time',$filed)){
            $db->order("add_time",'desc');
        }

        $whereArr=[];
        foreach ($this->param as $key=> $item) {
            if(in_array($key,$filed)&&(!empty($item)||$item===0||$item==='0')){
                $field_type = $fieldsType[$key];
                if($field_type=='int'||$field_type=='tinyint'){
                    $whereArr[]=["$key",'=',$item];
                }else{
                    $whereArr[]=["$key",'like','%'.$item.'%'];
                }
            }
        }

        $paginate = $db->where($whereArr)->paginate($this->param['limit'])->toArray();
        $list = $paginate['data'];
        if(!empty($join_table)){
            foreach ($list as &$item) {
                $item["{$join_table}_info"]=Db::name($join_table)->where("{$join_table}_id",$item["{$join_table}_id"])->find();
            }
        }

        // 需要单独处理的表
        if( $table=='role' ){
            foreach ($list as &$item) {
                $item["permission_ids"]=json_decode($item["permission_ids"],true);
            }
        }

        if( $table=='opera' ){
            foreach ($list as &$item) {
                $item["opera_focus_info"] = Db::name('opera_focus')->where('user_id', $this->user_id)->where('opera_id', $item["opera_id"])->count();
            }
        }

        $paginate['data']=$list;
        return $this->success('请求成功',[
            'paginate'=>$paginate,
//            '$filed'=>$filed,
//            '$fieldsType'=>$fieldsType,
            '$whereArr'=>$whereArr,
        ]);
    }
    public function save()
    {
        $row = $this->param;
        $table=$this->param['table'];
        $table_key = $table . '_id';
        $table_id = $this->param[$table_key] ?? null;
        $filed = Db::name($table)->getTableFields();

        if(!$table_id){
            if(in_array('add_time',$filed)){
                $row['add_time']=time();
            }
        }
        if(in_array('update_time',$filed)){
            $row['update_time']=time();
        }

        if( $table=='user' ){
            if($this->param['is_agent']&&!$this->param['agent_time']){
                $row['agent_time']=time();
            }
        }

        if( $table=='operalog' ){
            Db::name('operalog')->where('user_id',$this->user_id)->where('opera_collection_id',$row['opera_collection_id'])->delete();
        }

        if( $table=='opera_focus' ){
            $has = Db::name('opera_focus')->where('user_id',$this->user_id)->where('opera_id',$this->param['opera_id'])->find();
            if($has){
                Db::name('opera_focus')->where('opera_focus_id',$has['opera_focus_id'])->delete();
                return $this->ajax_return(1,'取消收藏成功',[
                ]);
            }else{
                Db::name('opera_focus')->strict(false)->save($row);
                return $this->ajax_return(1,'收藏成功',[
                ]);
            }
        }


        Db::name($table)->strict(false)->save($row);

        return $this->success('保存成功',[
        ]);
    }
    public function delete()
    {
        $table=$this->param['table'];
        Db::name($table)->delete($this->param['table_id']);
        if( $table=='category' ){
            Db::name('opera')->where('category_id',$this->param['table_id'])->delete();
        }
        if( $table=='opera' ){
            Db::name('opera_collection')->where('opera_id',$this->param['table_id'])->delete();
            Db::name('operalog')->where('opera_id',$this->param['table_id'])->delete();
            Db::name('opera_focus')->where('opera_id',$this->param['table_id'])->delete();
            Db::name('vmoneylog')->where('oid',$this->param['table_id'])->delete();
        }
        if( $table=='opera_collection' ){
            Db::name('operalog')->where('opera_collection_id',$this->param['table_id'])->delete();
            Db::name('vmoneylog')->where('cid',$this->param['table_id'])->delete();
        }

        return $this->success('删除成功');
    }
    public function detail()
    {
        $table=$this->param['table'];

        $detail = Db::name($this->param['table'])->find($this->param['table_id']);

        if( $table=='opera' ){
            $detail['opera_collection_count'] = Db::name('opera_collection')->where('opera_id',$this->param['table_id'])->count();
        }

        return $this->success('请求成功',[
            'detail'=>$detail,
        ]);
    }
    public function batchDelete()
    {
        $table=$this->param['table'];
        Db::name($this->param['table'])->delete($this->param['table_ids']);
        if( $table=='category' ){
            Db::name('opera')->where('category_id','in',$this->param['table_ids'])->delete();
        }
        if( $table=='opera' ){
            Db::name('opera_collection')->where('opera_id','in',$this->param['table_id'])->delete();
        }

        return $this->success('删除成功');
    }

}
